//
//  MusicDetailViewController.m
//  EDPS
//
//  Created by dllo on 15/12/1.
//  Copyright © 2015年 张维扬. All rights reserved.
//

#import "MusicDetailViewController.h"
#import "UIImageView+WebCache.h"
#import "UIImage+BlurAlpe.h"
#import "PlayData.h"
#import "STKAudioPlayer.h"
#import "ZWY_NetHandle.h"
#import "PlayData.h"
#import "SingleModel.h"
#import "STKDataSource.h"


@interface MusicDetailViewController ()

@property (nonatomic,retain) UIImageView *imageV;
@property (nonatomic,retain) UIImageView *CireleImage;
@property (nonatomic,retain) STKDataSource *dataSource;
@property (nonatomic,retain) STKAudioPlayer *player;
@property (nonatomic,retain) NSTimer *timer;
@property (nonatomic,retain) PlayData *playData;
@property (nonatomic,retain) UISlider *MusicSlider;
@property (nonatomic,retain) UILabel *timeLabel;
@property (nonatomic,retain) UISlider *prog;

@end

@implementation MusicDetailViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStyleDone target:self action:@selector(leftAction)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor blackColor];
    self.player = [[STKAudioPlayer alloc] initWithOptions:(STKAudioPlayerOptions){ .flushQueueOnSeek = YES, .enableVolumeMixer = YES, .equalizerBandFrequencies = {50, 100, 200, 400, 800, 1600, 2600, 16000} }];
    self.player.volume = 0.5;
    
    [self creatViews];
    [self playMusic];
    
}
+(instancetype)sharePlayerViewController
{
    
    static MusicDetailViewController *player = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        player = [[MusicDetailViewController alloc] init];
        
    });
    return player;
}

- (void)creatViews
{
    SingleModel *Smodel = [self.dataArr objectAtIndex:self.indexRow];
//背景图片
    NSString *CircleimageName = Smodel.coverLarge;
    self.imageV = [[UIImageView alloc] initWithFrame:self.view.frame];
    self.imageV.image = [self.imageV.image boxblurImageWithBlur:0.8];
    [self.imageV sd_setImageWithURL:[NSURL URLWithString:CircleimageName] placeholderImage:[UIImage imageNamed:@"img_my_bg@2x.jpg"]];
    self.imageV.image = [self.imageV.image boxblurImageWithBlur:0.8];
    [self.view addSubview:self.imageV];
//圆图片
    self.CireleImage = [[UIImageView alloc] initWithFrame:CGRectMake((ScreenWidth - 200)/2, 120, 200, 200)];
    self.CireleImage.layer.cornerRadius = 100;
    self.CireleImage.layer.masksToBounds = YES;
    [self.CireleImage sd_setImageWithURL:[NSURL URLWithString:CircleimageName] placeholderImage:[UIImage imageNamed:@"iconfont-ttpodicon.png"]];
    self.CireleImage.animationDuration = 20;
    self.CireleImage.animationRepeatCount = 0;
    [self.view addSubview:self.CireleImage];

//上一首
    UIButton *upButton = [UIButton buttonWithType:UIButtonTypeCustom];
    upButton.frame = CGRectMake(20,ScreenHeight - 140,80,60);
    [upButton setImage:[UIImage imageNamed:@"iconfont-shangyishou-2.png"] forState:UIControlStateNormal];
    [upButton addTarget:self action:@selector(Up) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:upButton];
//下一首
    UIButton *downButton = [UIButton buttonWithType:UIButtonTypeCustom];
    downButton.frame = CGRectMake(ScreenWidth - 40 - 80, ScreenHeight - 140 , 80, 60);
    [downButton setImage:[UIImage imageNamed:@"iconfont-player-next.png"] forState:UIControlStateNormal];
    [downButton addTarget:self action:@selector(Down) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:downButton];
//停止
    UIButton *stopButton = [UIButton buttonWithType:UIButtonTypeCustom];
    stopButton.frame = CGRectMake((ScreenWidth - 80)/2, ScreenHeight - 140, 80, 60);
    [stopButton setImage:[UIImage imageNamed:@"iconfont-tingzhiiiii.png"] forState:UIControlStateNormal];
    [stopButton setImage:[UIImage imageNamed:@"iconfont-kaishi.png"] forState:UIControlStateSelected];
    [stopButton addTarget:self action:@selector(StopS:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:stopButton];
    
/*
//设置音量
    self.MusicSlider = [[UISlider alloc] initWithFrame:CGRectMake(40, ScreenHeight - 130 ,ScreenWidth - 80 , 30)];
    self.MusicSlider.minimumValue = 0;
    self.MusicSlider.maximumValue = 1.1;
    //  [self.MusicSlider addTarget:self action:@selector(volumeChange:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:self.MusicSlider];
*/
    
//UIProgressView.进度条   歌曲进度
//self.prog
    
    self.prog = [[UISlider alloc] init];
    [self.prog setFrame:CGRectMake(40, ScreenHeight - 190 ,ScreenWidth - 80 , 30)];
    [self.prog addTarget:self action:@selector(despre:) forControlEvents:UIControlEventTouchUpInside];
    [self.imageV addSubview:self.prog];
    
//显示时间
    self.timeLabel = [[UILabel alloc] initWithFrame:CGRectMake( ScreenWidth - 40 - 120,ScreenHeight - 240, 120, 50)];
    [self.view addSubview:self.timeLabel];
    
    
}
- (void)Up
{
    if (self.indexRow == 0)
    {
        self.indexRow = self.dataArr.count - 1;
    }
    else
    {
        self.indexRow --;
    }
    [self layoutImage];
    [self playMusic];
}
- (void)Down
{
    if (self.indexRow == self.dataArr.count - 1)
    {
        self.indexRow = 0;
    }
    else
    {
        self.indexRow ++;
    }
    [self layoutImage];
    [self playMusic];
}
- (void)StopS:(UIButton *)button
{
    /*
    if (self.player.state == STKAudioPlayerStatePaused)
    {
        
        NSLog(@"暂停");
        [self.player resume];
        
    }else if (self.player.state == STKAudioPlayerStatePlaying)
    {
        
        NSLog(@"播放");
        [self.player pause];
        
    }else
    {
        [self playMusic];
    }
    */
    
    
    
    
    
    if (button.selected)
    {
       // [self playMusic];
        [self.player resume];
        //[self playMusic];
        NSLog(@"进行");
        button.selected = !button.selected;
    }
    else
    {
       // [self.player resume];
        [self.player pause];
       // [self.player clearQueue];//清空音频队列
        [self.timer invalidate];//定时器停止
      //  self.timer = nil;
       // self.prog.value = 0.f;
        NSLog(@"停止");
        button.selected = !button.selected;
    }
    
}
- (void)layoutImage
{
    SingleModel *Smodel = [self.dataArr objectAtIndex:self.indexRow];
    //背景图片
    NSString *CircleimageName = Smodel.coverLarge;
    self.imageV.image = [self.imageV.image boxblurImageWithBlur:0.3];
    [self.imageV sd_setImageWithURL:[NSURL URLWithString:CircleimageName] placeholderImage:[UIImage imageNamed:@"img_my_bg@2x.jpg"]];
    self.imageV.image = [self.imageV.image boxblurImageWithBlur:0.3];
    //圆图片
    self.CireleImage.layer.cornerRadius = 100;
    self.CireleImage.layer.masksToBounds = YES;
    [self.CireleImage sd_setImageWithURL:[NSURL URLWithString:CircleimageName] placeholderImage:[UIImage imageNamed:@"iconfont-ttpodicon.png"]];
    
}
- (void)playMusic
{
    SingleModel *model = [self.dataArr objectAtIndex:self.indexRow];
    NSInteger song_id = (NSInteger )model.trackId;
    NSString *string = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/track/detail?device=iPhone&trackId=%ld",song_id];
    NSLog(@"song_id = %ld",song_id);
    [ZWY_NetHandle getURL:string Cookie:nil parameter:nil result:^(id result) {
        NSMutableDictionary *dictionary = result;
        PlayData *playData = [[PlayData alloc] init];
        [playData setValuesForKeysWithDictionary:dictionary];
        self.playData = playData;
        //播放音频url
        [self.player play:playData.playUrl64];
        self.navigationItem.title = playData.title;
        
        self.timer = [NSTimer scheduledTimerWithTimeInterval:1.f target:self selector:@selector(track) userInfo:nil repeats:YES];
    }];
}
-(void)track
{
    self.prog.maximumValue = self.player.duration;//音乐总共时长
    self.prog.value = self.player.progress;//当前进度
    //当前时长进度progress
    NSInteger proMin = (NSInteger)self.player.progress / 60;//当前秒
    NSInteger proSec = (NSInteger)self.player.progress % 60;//当前分钟
    //duration 总时长
    NSInteger durMin = (NSInteger)self.player.duration / 60;//总秒
    NSInteger durSec = (NSInteger)self.player.duration % 60;//总分钟
    self.timeLabel.text = [NSString stringWithFormat:@"%02ld:%02ld / %02ld:%02ld", proMin, proSec, durMin, durSec];
    
}
- (void)despre:(UISlider *)slider
{
    if (self.player)
    {
        [self.player seekToTime:slider.value];
    }
}

- (void)leftAction
{
    [self.navigationController popViewControllerAnimated:YES];
}


@end